<script setup>
import { ElMessage } from "element-plus";
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import hxxp from "@/utils/http";

//创建一个路由
const router = useRouter();
const store = useStore();

const data = reactive({
  systemUser: {},
})

const login = () => {
  hxxp.post('/login/checkLogin', data.systemUser).then(res => {
    if (res.code === 100200) {
      ElMessage({
        message: res.msg,
        type: "success"
      })
      sessionStorage.setItem('systemUser', JSON.stringify(res.data))

      // 设置当前用户到store
      store.commit('setCurrentUser', res.data)

      // 清除所有标签，为新用户重新开始
      store.commit('clearTags')

      router.push('/welcome')
    } else {
      ElMessage({
        message: res.msg,
        type: "error"
      })
    }
  })
}
</script>

<template>
  <div class="login-page">
    <div class="login-container">
      <div class="login-box">
        <div class="login-header">
          <img src="@/assets/images/hemiao.png" alt="Logo" class="logo">
          <h1 class="title">农业保险管理系统</h1>
          <p class="subtitle">守护三农生产 · 风险保障与贴心服务</p>
        </div>

        <el-form :model="data.systemUser" class="login-form">
          <el-form-item>
            <el-input v-model="data.systemUser.username" placeholder="用户名" size="large" class="input-field">
              <template #prefix>
                <el-icon>
                  <User />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-input v-model="data.systemUser.password" type="password" placeholder="密码" size="large"
                      class="input-field" show-password>
              <template #prefix>
                <el-icon>
                  <Lock />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="login" size="large" class="login-btn">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  min-height: 100vh;
  background:
      radial-gradient(1200px 600px at 10% 10%, rgba(255, 255, 255, 0.18), transparent 60%),
      radial-gradient(900px 500px at 90% 20%, rgba(255, 255, 255, 0.12), transparent 60%),
      linear-gradient(135deg, #9ad26d 0%, #5db060 60%, #518454 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-container {
  width: 100%;
  max-width: 400px;
}

.login-box {
  background: white;
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  position: relative;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  margin-bottom: 12px;
  box-shadow: 0 8px 20px rgba(76, 175, 80, 0.25);
}

.title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.subtitle {
  margin-top: 8px;
  font-size: 13px;
  color: #5f7a65;
}

.login-form {
  width: 100%;
}

.input-field {
  width: 100%;
  margin-bottom: 20px;
}

.input-field :deep(.el-input__wrapper) {
  border-radius: 10px;
  border: 1px solid #d9e4d8;
  height: 48px;
  transition: box-shadow 0.2s ease, border-color 0.2s ease;
}

.input-field :deep(.el-input__wrapper:hover) {
  border-color: #66bb6a;
}

.input-field :deep(.el-input__wrapper.is-focus) {
  border-color: #43a047;
  box-shadow: 0 0 0 3px rgba(67, 160, 71, 0.12);
}

.login-btn {
  width: 100%;
  height: 48px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.5px;
  background: linear-gradient(135deg, #66bb6a 0%, #43a047 100%);
  border: none;
}

.login-btn:hover {
  opacity: 0.95;
  box-shadow: 0 8px 18px rgba(67, 160, 71, 0.24);
}

.login-form .el-form-item {
  margin-bottom: 0;
}

.login-form .el-form-item:last-child {
  margin-bottom: 0;
}

@media (max-width: 480px) {
  .login-box {
    padding: 30px 20px;
  }

  .title {
    font-size: 20px;
  }
}
</style>